package cn.hzy.admin.services.system.impl;

import cn.hzy.admin.controller.LoginController;
import cn.hzy.admin.models.SysUser;
import cn.hzy.admin.mapper.SysUserMapper;
import cn.hzy.admin.services.system.ISysUserService;
import cn.hzy.toolkit.JwtTokenUtil;
import cn.hzy.toolkit.MessageBox;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hzy
 * @since 2020-04-26
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    private final Logger log = LoggerFactory.getLogger(LoginController.class);
    private final HttpServletRequest request;

    public SysUserServiceImpl(HttpServletRequest _request) {
        this.request = _request;
    }

    /**
     * 检查账户信息
     *
     * @param sysUser
     * @return
     */
    @Override
    public String check(SysUser sysUser) {

        //        log.error("我是 测试 日志!");

        if (Strings.isEmpty(sysUser.getAccountName())) {
            MessageBox.show("请输入 账户名称!");
        }

        if (Strings.isEmpty(sysUser.getPassword())) {
            MessageBox.show("请输入 账户密码!");
        }

        var query = new QueryWrapper<SysUser>()
                .lambda()
                .eq(SysUser::getAccountName, sysUser.getAccountName());

        var sysUser1 = this.baseMapper.selectOne(query);

        if (sysUser1 == null) MessageBox.show("账户不存在!");

        //判断密码
        if (!sysUser1.getPassword().equals(sysUser.getPassword())) {
            MessageBox.show("账户密码错误!");
        }

        sysUser = sysUser1;

        //Jwt
        var token = JwtTokenUtil.sign(sysUser1, 60L * 1000L * 30L);

        return token;
    }

    /**
     * 获取 token
     *
     * @return
     */
    @Override
    public String getToken() {
        //验证 token
        var tokenKeyPrefix = "Authorization";
        var serverName = request.getServerName();
        var serverPort = request.getServerPort();
        var tokenKey = tokenKeyPrefix + "_" + serverName + "_" + serverPort;

        var cookies = request.getCookies();
        if (cookies == null) return null;

        var cookie = Arrays.stream(cookies).filter(w -> w.getName().equals(tokenKey)).findFirst().get();
        if (cookie == null) return null;

        return cookie.getValue();
    }

    /**
     * 获取账户信息
     *
     * @return
     */
    @Override
    public SysUser getAccount() {
        try {
            var tokenValue = this.getToken();

            if (tokenValue == null) return null;

            //验证 token 是否有效
            var sysUser = JwtTokenUtil.unsign(tokenValue, SysUser.class);

            return sysUser;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }

    }
}
